package com.hanhan.chargingpile.stations.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanhan.chargingpile.stations.mapper.ChargingPileMapper;
import com.hanhan.chargingpile.stations.pojo.entity.ChargingStations;
import com.hanhan.chargingpile.stations.mapper.ChargingStationsMapper;
import com.hanhan.chargingpile.stations.pojo.vo.ChargingStationsVO;
import com.hanhan.chargingpile.stations.service.ChargingStationsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 充电站 服务实现类
 * </p>
 *
 * @author HanHan
 * @since 2023-03-22
 */
@Service
public class ChargingStationsServiceImpl extends ServiceImpl<ChargingStationsMapper, ChargingStations> implements ChargingStationsService {

    @Resource
    private ChargingPileMapper chargingPileMapper;

    @Override
    public IPage<ChargingStationsVO> selectChargingStationsList(Page<ChargingStations> pageParam, ChargingStations chargingStations) {
        QueryWrapper<ChargingStations> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .like(chargingStations.getStationName() != null && chargingStations.getStationName() != "", "station_name", chargingStations.getStationName())
                .eq(chargingStations.getProvince() != null, "province", chargingStations.getProvince())
                .eq(chargingStations.getCity() != null, "city", chargingStations.getCity())
                .groupBy("id");
        return baseMapper.selectChargingStationsList(pageParam, queryWrapper);
    }

    @Override
    public ChargingStationsVO show(Long id) {
        QueryWrapper<ChargingStations> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        ChargingStations chargingStations = baseMapper.selectOne(queryWrapper);
        ChargingStationsVO chargingStationsVO = new ChargingStationsVO();
        chargingStationsVO.setId(chargingStations.getId());
        chargingStationsVO.setLongitude(chargingStations.getLongitude());
        chargingStationsVO.setLatitude(chargingStations.getLatitude());
        chargingStationsVO.setStationName(chargingStations.getStationName());
        chargingStationsVO.setStationType(chargingStations.getStationType());
        chargingStationsVO.setStationDetails(chargingStations.getStationDetails());
        chargingStationsVO.setProvince(chargingStations.getProvince());
        chargingStationsVO.setCity(chargingStations.getCity());
        chargingStationsVO.setCounty(chargingStations.getCounty());
        chargingStationsVO.setAddress(chargingStations.getAddress());
        chargingStationsVO.setPrice(chargingStations.getPrice());
        chargingStationsVO.setParkingFeeDetails(chargingStations.getParkingFeeDetails());
        chargingStationsVO.setHaveParkingFee(chargingStations.getHaveParkingFee());
        chargingStationsVO.setOpenStartTime(chargingStations.getOpenStartTime());
        chargingStationsVO.setOpenEndTime(chargingStations.getOpenEndTime());
        chargingStationsVO.setCoverImg(chargingStations.getCoverImg());
        chargingStationsVO.setGrSerialNumber(chargingStations.getGrSerialNumber());
        chargingStationsVO.setDisable(chargingStations.getDisable());
        chargingStationsVO.setChargingPileNum(chargingPileMapper.selectTotal(id));
        return chargingStationsVO;
    }
}
